<?php namespace Admin\Controller;
use Hdphp\Controller\Controller; 
/**
 * 商品控制器
 */
class GoodController extends CommonController{
    private $model;
	public function __init(){
		//调用CommonController里面的构造函数
		parent::__inin();
		//实例化模型类Good
		$this->model=new \Common\Model\Good;
	}
    //商品首页
    public function index(){
        $data = $this->model->get();
        View::with('data',$data);
       
    	View::make();
    }

    // 添加商品
    public function add(){
             if(IS_POST){
                // p($_POST);die;
                $_POST['pic'] =implode(',', Q('post.pic'));
              
                if(!$this->model->store()) View::error($this->model->getError());
                View::success('添加成功',U('index'));
            }


             // 实例化Cate模型
            $cateModel = new \Common\Model\Cate;
            $cate  = Data::tree($cateModel->get(),'cname');
            View::with('cate',$cate);

 
            // 实例化Brand模型
            $brandModel= new \Common\Model\Brand;
            View::with('brand',$brandModel->get());

             
            $gdetailModel = new \Common\Model\Gdetail;
            $gdetail = $gdetailModel->get();
            View::with('gdetail',$gdetail);
                // p($brandModel->get());



        // $goodsattr = new \Common\Model\Gnature;
        


            View::make();
    }


    // 删除商品
    public function del(){
            $gid = Q('get.gid',0,'intval');
            $this->model->where("gid={$gid}")->delete();
            
            View::success("删除成功");
    }
   
    // 商品编辑
//  修改
    public function edit(){
        if(IS_POST){
            // p($_POST);
            if($this->model->edit());  View::success('修改成功',U('index'));
           View::error($this->model->getError());
        }
        
//      接收gid 要知道修改的那条
        $gid = Q('get.gid',0,'intval');
        // p($gid);
//      获得旧内容
        $oldData = $this->model->where("gid={$gid}")->find();
        // p($oldData);
        View::with('oldData',$oldData);


        // 实例化Cate模型s
        $cateModel = new \Common\Model\Cate;
        $cate = Data::tree($cateModel->get(),'cname');
        // p($cate);
        View::with('cate',$cate);


        // 实例化Brand模型
        $brandModel = new \Common\Model\Brand;
        $brand =  $brandModel->get();
        View::with('brand',$brand);


        // 实例化Gnature模型
        $gnatureModel = new \Common\Model\Gnature;
        $gnatureModel  = $gnatureModel->where("good_gid={$gid}")->get();
//      p($gnatureModel);
        
  


        // 实例化gdetail
        $detailModel = new \Common\Model\Gdetail;
        
        $detailData = $detailModel->where("gaid={$oldData['type_tid']}")->find();
        $detailData['pic'] = explode(',', $detailData['pic']);
            View::with('detailData',$detailData);
        // View::with('gdetail',$gdetail);
//      
       View::make();
    }


































    // 规格属性异步 php做处理
    public function getTypeAttr(){
        $tid = Q('post.id',0,'intval');
        $data = Db::table('typeattr')->where("type_tid={$tid}")->get();
        foreach ($data as $k => $v) {
            $data[$k]['tavalue'] = explode(',', $v['tavalue']);
        }
        // 反馈给ajax
        echo json_encode($data);exit;
        
    }

    public function upload(){
    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
    if (empty($file)) {
        $this->ajax(Upload::getError());
    } else {
        /** $file内部就是以下这个数组
            $file = array(
                0 => array(
                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
                'image' => 1
            ),
        );**/
        //上传成功，把上传好的信息返给js
        $data = $file[0];
        echo json_encode($data);exit;
    }
}
}